package cn.ljn.basic.controller;


import cn.ljn.basic.dto.EmailDto;
import cn.ljn.basic.dto.PhoneCodeDto;
import cn.ljn.basic.dto.VerifyCodeDto;
import cn.ljn.basic.service.VerifyCodeService;
import cn.ljn.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;

/**
 * 验证码controller
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private VerifyCodeService verifyCodeService;

    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult imageCode(@PathVariable("imageCodeKey") String imageCode) throws IOException {
        String base64ImageStr = verifyCodeService.imageCode(imageCode);
        return AjaxResult.me().success(base64ImageStr);
    }

    /**
     * 发送手机验证码
     * @return
     * @throws IOException
     */
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody @Valid VerifyCodeDto dto){
        verifyCodeService.smsCode(dto);
        return AjaxResult.me().success();
    }
    /**
     *手机好注册
     */
    @PostMapping("/phoneRegister")
    public AjaxResult phoneRegister(@RequestBody @Valid PhoneCodeDto dto) {
        verifyCodeService.phoneRegister(dto);
        return AjaxResult.me().success();
    }

    /**
     * 邮箱注册
     */

    @PostMapping("/emailRegister")
    public AjaxResult emailRegister(@RequestBody @Valid EmailDto dto) {
        verifyCodeService.emailRegister(dto);
        return AjaxResult.me().success();
    }
}
